/**
 * 安全警报 列表页
 *
 * 作者 zhangwei
 * 日期 2024-08-22 09:19:50
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-accidentAlerts'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#accidentAlerts-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();
    initDate();

    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#publishDate-range',
            type: 'date',
            range: ['#publishStartDate', '#publishEndDate'],
        })

        laydate.render({
            elem: '#deadlineDate-range',
            type: 'date',
            range: ['#deadlineStartDate', '#deadlineEndDate'],
        })
    }

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'accidentAlertsTable',
            url: ctx + 'accidentAlerts/list',
            where: getQueryParams(),
            height:'full-120',
            skin: '',
            cols: [[
                {title: '序号',align:'center', type: 'numbers',width: 60},
                {field: 'tenantName', title: '公司名称', width: 200,hide:!hasHeadCompanyRole},
                {field: 'alertsType',align:'center', title: '警报类型', width: 100, templet:function (d){
                    if(d.alertsType == '1'){
                        return  "系统内";
                    }else if(d.alertsType == '2'){
                        return "系统外";
                    }
                    }},
                {field: 'alertsTitle', title: '警报主题',style:'color:#01aaed;cursor:pointer',event:'detail'},
                {field: 'publishDate',align:'center', title: '发布时间', width: 120},
                {field: 'deadlineDate',align:'center', title: '截至时间', width:120},
                {field: 'status',align:'center', title: '状态', width: 105,toolbar: '#status-bar'},
                {title: '操作',align:'center', fixed:'right', toolbar: '#accidentAlerts-tool', width: 160}
            ]],
            toolbar: '#accidentAlerts-toolbar',
            defaultToolbar: []
        });
        tableIns.reload({url: ctx + 'accidentAlerts/list', where: getQueryParams(), page: {curr: 1}});
    }

    table.on('sort(accidentAlertsTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(accidentAlertsTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增', 'accidentAlerts/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['95%', '95%']: ['95%', '95%'],
                yes: function (index, layero) {
                    $('#accidentAlerts-edit').find('#submit').trigger('click');
                    tinyMCE.remove('#alertsContent');
                },
                btn2: function () {
                    tinyMCE.remove('#alertsContent');
                    layer.closeAll();
                },
                cancel:function () {
                    tinyMCE.remove('#alertsContent');
                },
                data: {
                    tenantId: $tenantSelect.val(),
                }
            });
        }
    });

    table.on('tool(accidentAlertsTable)', function (obj) {
        var data = obj.data;
        var alertsId = data.alertsId;
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.alertsId);
            });
        }else if (layEvent === 'edit') {
            febs.modal.open('修改', 'accidentAlerts/update/' + data.alertsId, {
                area: $(window).width() <= 750 ? ['95%', '95%']: ['95%', '95%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    tinyMCE.remove('#alertsContent');
                    $('#accidentAlerts-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    tinyMCE.remove('#alertsContent');
                    layer.closeAll();
                },
                data: {
                    tenantId: $tenantSelect.val(),
                },
            });
        }else if(obj.event === 'detail'){
            febs.modal.open('安全警报详情', 'accidentAlerts/detail/'+alertsId, {
                btn: ['关闭'],
                area: $(window).width() <= 750 ?  ['85%', '80%']: ['1300px', '95%'],
                shadeClose:true,
                yes: function (index, layero) {
                    layer.closeAll();
                },
                cancel:function () {
                },
            });
        }
    });

    function getQueryParams() {
        return {
            tenantId: $searchForm.find("select[name='tenantSelect']").find("option:selected").val(),
            alertsType: $searchForm.find("select[name='alertsType']").find("option:selected").val(),
            alertsTitle: $searchForm.find('input[name="alertsTitle"]').val()?.trim(),
            publishStartDate: $searchForm.find('input[name="publishStartDate"]').val()?.trim(),
            publishEndDate: $searchForm.find('input[name="publishEndDate"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'accidentAlerts/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    form.on('switch(alertsStatus)', function (obj) {
        //区域状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其 postId
        var id =  table.cache['accidentAlertsTable'][index].alertsId;
        console.log("事项Id"+id);
        febs.modal.confirm('更改状态', '确认更改状态',function (){
            updateStatus(id,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });
    function updateStatus(id,status) {
        //更新启用状态
        let model={
            alertsId : id,
            status : status
        }
        febs.post(ctx + 'accidentAlerts/updateStatus', model, function () {
            febs.alert.success('修改状态成功');
            reloadPage(1);
        })
    }

});